﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
using System.Web.Services;
using System.Collections;

namespace GeniusWebApplication
{
    public partial class Keywords : System.Web.UI.Page
    {
        public static User user = null;
        public static int categoryId = 1;
        public string key = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["keys"] != null)
            {
                key=Request["keys"];
            }
            user = (User)HttpContext.Current.Session["user"];
            if (user == null)
            { Response.Redirect("log.aspx"); }




            //初始化关键字
            IList<Keyword> kl = new KeywordService().getKeywords();
            List<string> keyStringList = new List<string>();
            List<int> keyStringListBig = new List<int>();
            foreach (Keyword k in kl)
            {

                if (keyStringList.Count() == 0)
                {
                    keyStringList.Add(k.keywordName);
                    keyStringListBig.Add(5);
                }
                else if (keyStringList.FindIndex(delegate(string s) { return s == k.keywordName; }) == -1)
                {
                    keyStringList.Add(k.keywordName);
                    keyStringListBig.Add(5);
                }
                else
                {
                    int ii = keyStringList.FindIndex(delegate(string s) { return s == k.keywordName; });
                    if (keyStringListBig[ii] >= 2)
                    {
                        keyStringListBig[ii] = keyStringListBig[ii] - 1;
                    }
                }
            }

            for (int m = 0; m < keyStringList.Count(); m++)
            {
                this.keys.InnerHtml = this.keys.InnerHtml + "<div class='keysItem'><p class='ggz" + keyStringListBig[m] + "'><a href='./keywords.aspx?keys=" + keyStringList[m] + "'>" + keyStringList[m] + "</a></p></div>";
            }

            //初始化关键字相关资源
            ResourceService resourceS = new ResourceService();

            FavoriteService fss = new FavoriteService();
            IList<Resource> rl = new ResourceService().getResourceByKeyword(key);

            foreach (Resource c in rl)
            {
                if (c.status == 1)
                {
                    if (c.type == 2)
                    {
                        if (fss.getFavourite(user, c) == null)
                        {
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item.aspx?resourceId=" + c.id + "'>" + c.fileName + "</a></span><span class='myListUl-text'><span id='myListUl-img1' ><a href='./download.aspx?resourceId=" + c.id + "'><img src='./img/opt_dow.png' title='下载'></a></span><span id='myListUl-img2' ><img src='./img/opt_star.png' title='收藏' onclick='clickFab(this);' id='" + c.id + "'></span></span> </li>";
                        }
                        else
                        {
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item.aspx?resourceId=" + c.id + "'>" + c.fileName + "</a></span><span class='myListUl-text'><span id='myListUl-img1' ><a href='./download.aspx?resourceId=" + c.id + "'><img src='./img/opt_dow.png' title='下载'></a></span><span id='myListUl-img2' ><img src='./img/opt_fab.png' title='收藏' onclick='clickFab(this);' id='" + c.id + "'></span></span> </li>";
                        }
                    }
                    else if (c.type == 1)
                    {
                        if (fss.getFavourite(user, c) == null)
                        {
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_media.aspx?resourceId=" + c.id + "'>" + c.fileName + "</a></span><span class='myListUl-text'><span id='myListUl-img1' ><a href='./download.aspx?resourceId=" + c.id + "'><img src='./img/opt_dow.png' title='下载'></a></span><span id='myListUl-img2' ><img src='./img/opt_star.png' title='收藏' onclick='clickFab(this);' id='" + c.id + "'></span></span> </li>";
                        }
                        else
                        {
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_media.aspx?resourceId=" + c.id + "'>" + c.fileName + "</a></span><span class='myListUl-text'><span id='myListUl-img1' ><a href='./download.aspx?resourceId=" + c.id + "'><img src='./img/opt_dow.png' title='下载'></a></span><span id='myListUl-img2' ><img src='./img/opt_fab.png' title='收藏' onclick='clickFab(this);' id='" + c.id + "'></span></span> </li>";
                        }
                    }
                    else if (c.type == 3)
                    {
                        if (fss.getFavourite(user, c) == null)
                        {
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_else.aspx?resourceId=" + c.id + "'>" + c.fileName + "</a></span><span class='myListUl-text'><span id='myListUl-img1' ><a href='./download.aspx?resourceId=" + c.id + "'><img src='./img/opt_dow.png' title='下载'></a></span><span id='myListUl-img2' ><img src='./img/opt_star.png' title='收藏' onclick='clickFab(this);' id='" + c.id + "'></span></span> </li>";
                        }
                        else
                        {
                            this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span ><a href='./item_else.aspx?resourceId=" + c.id + "'>" + c.fileName + "</a></span><span class='myListUl-text'><span id='myListUl-img1' ><a href='./download.aspx?resourceId=" + c.id + "'><img src='./img/opt_dow.png' title='下载'></a></span><span id='myListUl-img2' ><img src='./img/opt_fab.png' title='收藏' onclick='clickFab(this);' id='" + c.id + "'></span></span> </li>";
                        }
                    }
                }
            }
                
            

        }

        [WebMethod]
        public static string ajaxSaveFab(int resourceId)
        {

            FavoriteService fService = new FavoriteService();

            Hashtable ht = fService.addFavourite(user, resourceId);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";

        }
        [WebMethod]
        public static string ajaxDelFab(int resourceId)
        {

            FavoriteService fService = new FavoriteService();
            IList<Favourite> f = fService.getUserFavourite(user.id);
            Hashtable ht = new Hashtable();
            foreach (Favourite mf in f)
            {
                if (mf.resource.id == resourceId)
                {
                    ht = fService.delFavourite(mf.id);
                }
            }
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";

        }
    }
}